<?php
namespace Work\Controller;
use Admin\Controller\AdminbaseController;
class IndexController extends AdminbaseController {
	
	protected $m;
	protected $mm;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('YgclWork');
		$this->mm = D('YgclLesson');
	}
	
    public function index(){
    	if(!$_GET['status']){
    		$_GET['status'] = 0;
    	}
    	$this->assign('curUser',$this->curUser);
    	$this->display(); 
    }
    
    public function edit(){
    	$lesson = $this->mm->where(array('cid'=>I('cid'),'status'=>1))->select();
    	$this->lesson = $lesson;
    	
    	//学年信息
    	$xuenian = M('YgclTermTime')->where(array('type'=>1))->order('id desc')->select();
    	$this->xuenian = $xuenian;
    	
    	$id = I('id');
    	if($id){
    		$this->info = $this->m->where(array('id'=>$id))->find();
    	}
    	$this->display();
    }
    
    public function edit_post(){
    	if(IS_POST){
    		$_POST['update_time'] = Date('Y-m-d H:i:s');
    		$_POST['uid'] = session('loginUser')['uid'];
    		if($_POST['id']){
    			if($this->m->create()){
    				$id = $_POST['id'];
    				if($this->m->save()){
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}
    		}else{
    			$_POST['create_time'] = Date('Y-m-d H:i:s');
    			if($this->m->create()){
    				$id = $this->m->add();
    				if($id){
    					$this->success('添加成功');
    				}else{
    					$this->error('添加失败');
    				}
    			}
    		}
    	}
    }
    
    public function del(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	if($this->m->where($map)->delete()){
    		$this->success('删除成功');
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function changestatus(){
    	$data[$this->m->getPk()] = $_REQUEST['id'];
    	$data['status'] = $_REQUEST['status'];
    	$data['update_time'] = date('Y-m-d H:i:s');
    	if($this->m->save($data)){
    		$this->success('状态更新成功');
    	}else{
    		$this->error('状态更新失败');
    	}
    }
}